package com.qdlc.p2p.dal.dao.impl;

import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.CashAwardRuleDao;
import com.qdlc.p2p.dal.dao.OperatorDao;
import com.qdlc.p2p.dal.dto.CashAwardRule;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.model.CashAwardRuleModel;
import com.qdlc.p2p.dal.mybatis.model.mapper.CashAwardRuleMapper;
import com.qdlc.p2p.dal.mybatis.pager.PageProxyBuilder;
import com.qdlc.p2p.dal.mybatis.pager.core.PageParameter;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 现金奖励规则DAO接口实现类
 *
 * @author Kaiju Zhao
 * @date 2016-3-23
 */
@Repository(value = "cashAwardRuleDao")
public class CashAwardRuleDaoImpl implements CashAwardRuleDao {

    @Resource
    CashAwardRuleMapper cashAwardRuleMapper;

    @Resource
    OperatorDao operatorDao;


    @Override
    public PageDataList<CashAwardRuleModel> list(CashAwardRuleModel model) {
        PageProxyBuilder<CashAwardRuleMapper> proxy = PageProxyBuilder.newBuilder(CashAwardRuleMapper.class);
        proxy.setPageParameter(model.getPage(), model.getRows());
        List<CashAwardRule> modelList;
        modelList = proxy.createProxy().findByModel(model);
        PageParameter pageParameter = proxy.getPageParameter();
        PageDataList<CashAwardRule> dataList = new PageDataList<CashAwardRule>(pageParameter.convert2Page(), modelList);
        PageDataList<CashAwardRuleModel> modelDataList = new PageDataList<CashAwardRuleModel>();
        List<CashAwardRule> ruleList = dataList.getList();
        List<CashAwardRuleModel> list = new ArrayList<CashAwardRuleModel>();
        if (null != ruleList
                && ruleList.size() > 0) {
            for (CashAwardRule entity : ruleList) {
                CashAwardRuleModel cashAwardRuleModel = CashAwardRuleModel.instance(entity);
                if (entity.getModifier()!=0) {
                    Operator operator = operatorDao.userFind(entity.getModifier());
                    cashAwardRuleModel.setModifierName(operator.getUserName());
                }
                if (entity.getCreator()!=0) {
                    Operator operator = operatorDao.userFind(entity.getCreator());
                    cashAwardRuleModel.setCreatorName(operator.getUserName());
                }
                list.add(cashAwardRuleModel);
            }
        }
        modelDataList.setPage(dataList.getPage());
        modelDataList.setList(list);
        return modelDataList;
    }

    @Override
    public int toggleStatus(CashAwardRuleModel model) {
        return cashAwardRuleMapper.toggleStatus(model);
    }

    @Override
    public int save(CashAwardRule cashAwardRule) {
        return cashAwardRuleMapper.insert(cashAwardRule);
    }

    @Override
    public CashAwardRule find(long id) {
        return cashAwardRuleMapper.findById(id);
    }

    @Override
    public int update(CashAwardRule cashAwardRule) {
        return cashAwardRuleMapper.update(cashAwardRule);
    }

    @Override
    public List<CashAwardRule> findByStatus(int status) {
        return cashAwardRuleMapper.findByStatus(status);
    }

}
